package com.example.weatherxml;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends Activity implements OnClickListener {
	
	static final String baseURL = "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=";
	TextView tv;
	EditText city, state;
	

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
   Button b = (Button)findViewById(R.id.btnDeilingRes);
   tv = (TextView)findViewById(R.id.textView1);
   city = (EditText)findViewById(R.id.city);
   state = (EditText)findViewById(R.id.state);
   b.setOnClickListener(this);
   
    }


	public void onClick(View v) {
		// TODO Auto-generated method stub
		String c = city.getText().toString();
		String s = state.getText().toString();
		
		StringBuilder URL = new StringBuilder(baseURL);
		URL.append(c);
		String fullUrl = URL.toString();
		
		try{
			URL website = new URL(fullUrl);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			HandlingXMLStuff doingWork = new HandlingXMLStuff();
			xr.setContentHandler(doingWork);
			xr.parse(new InputSource(website.openStream()));
			String information = doingWork.getInformation();
			tv.setText(information);
		}catch (Exception e){
		tv.setText("error");
		}
		
	}
}

   
